package com.hspedu.tankgame5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import java.io.File;

//为了监听键盘事件，实现KeyListener
//为了让Panel 不停的重绘子弹，需要MyPanel实现Runnable，当做一个线程使用
public class TankPanel extends JPanel implements KeyListener,Runnable {
    Hero hero = null;

    Vector<EnemyTank> enemyTanks = new Vector<>();
    //定义一个Vector，用于存放炸弹
    Vector<Node> nodes = new Vector<>();

    Vector<Bomb> bombs = new Vector<>();
    int enemyTankSize = 8;

    //定义三张炸弹图片，用于显示爆炸效果
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public TankPanel(String key){
        File file = new File(Recorder.getRecordFile());
        if(file.exists()){
            nodes = Recorder.getNodesAndEnemyRec();
        }else{
            System.out.println("文件不存在,只能开启新游戏");
            key = "1";
        }
        Recorder.setEnemyTanks(enemyTanks);
        hero = new Hero(100,100,0); //初始化自已的坦克
        switch (key){
            case "1":
                // 初始化敌人的坦克
                for(int i=0;i<enemyTankSize;i++){
                    // 创建一个敌人的坦克
                    EnemyTank enemyTank = new EnemyTank(100*(i+1),0,2);
                    enemyTank.setEnemyTanks(enemyTanks);

                    //启动敌人坦克线程，让他动起来
                    new Thread(enemyTank).start();
                    // 创建子弹
                    //Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60,enemyTank.getDirect());
                    // 子弹加入Vertor集合
                    //enemyTank.shots.add(shot);
                    // 启动线程
                    //new Thread(shot).start();

                    enemyTanks.add(enemyTank);
                }
                break;
            case "2": //继续上局游戏
                // 初始化敌人的坦克
                for(int i=0;i< nodes.size();i++){
                    // 创建一个敌人的坦克
                    Node node = nodes.get(i);
                    EnemyTank enemyTank = new EnemyTank(node.getX(),node.getY(),node.getDirect());
                    enemyTank.setEnemyTanks(enemyTanks);

                    //启动敌人坦克线程，让他动起来
                    new Thread(enemyTank).start();
                    // 创建子弹
                    //Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60,enemyTank.getDirect());
                    // 子弹加入Vertor集合
                    //enemyTank.shots.add(shot);
                    // 启动线程
                    //new Thread(shot).start();

                    enemyTanks.add(enemyTank);
                }
                break;
            default:
                System.out.println("你的输入有误");

        }
        System.out.println("when you create hero instance:" + hero.isLive);
        hero.setSpeed(2);

        //初始化图片对象

        image1 = Toolkit.getDefaultToolkit().getImage(TankPanel.class.getResource("bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(TankPanel.class.getResource("bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(TankPanel.class.getResource("bomb_3.gif"));
        new AePlayWave("d:\\111.wav").start();
    }

    public void showInfo(Graphics g){
        g.setColor(Color.BLACK);
        Font font = new Font("宋体",Font.BOLD,25);
        g.setFont(font);
        g.drawString("您累积击毁敌方坦克",1020,30);
        g.setColor(Color.BLACK);
        drawTank(1020,60,g,0,0);
        g.setColor(Color.BLACK);
        g.drawString(Recorder.getAllEnemyTankNum()+ "",1080,100);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);
        showInfo(g);
        if(hero != null && hero.isLive) {
            System.out.println("tank isLive Status:" + hero.isLive);
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
        }else{
            System.out.println("maybe after hero was been kill");
            System.out.println(hero.isLive);


        }
        //画出hero射击的子弹
//        if(hero.shot != null && hero.shot.isLive == true){
//            System.out.println("子弹被绘制...");
//            g.draw3DRect(hero.shot.x,hero.shot.y,2,2,false);
//        }

        // 将 hero的子弹集合shots,遍历取出绘制
        for(int i = 0;i < hero.shots.size();i++) {
            Shot shot = hero.shots.get(i);
            if(shot != null && shot.isLive){
                g.draw3DRect(shot.x,shot.y,2,2,false);
            }else{
                hero.shots.remove(shot);
            }
        }


        for(int i=0;i<bombs.size();i++){
            Bomb bomb = bombs.get(i);
            if(bomb.life > 6){
                g.drawImage(image1,bomb.x, bomb.y,60,60,this);
            }else if(bomb.life > 3){
                g.drawImage(image2,bomb.x, bomb.y,60,60,this);
            }else {
                g.drawImage(image3,bomb.x, bomb.y,60,60,this);
            }

            bomb.lifeDown();

            if(bomb.life == 0){
                bombs.remove(bomb);
            }
        }

        // 画出敌人坦克
        for(int i=0;i<enemyTanks.size();i++){
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),0);
            // 画出enemyTank 所有子弹,一个坦克多颗子弹
            for(int j = 0; j<enemyTank.shots.size();j++){
                //取出子弹
                Shot shot = enemyTank.shots.get(j);
                if(shot.isLive){
                    g.draw3DRect(shot.x, shot.y,1,1,false);
                } else {
                    enemyTank.shots.remove(shot);
                }

            }
            }
        }
    }
    public void hitHero(){
        for(int i=0;i<enemyTanks.size();i++){
            EnemyTank enemyTank = enemyTanks.get(i);
            for(int j=0;j< enemyTank.shots.size();j++){
                Shot shot = enemyTank.shots.get(j);
                if(hero.isLive && shot.isLive){
                    hitTank(shot,hero);
                }
            }
        }
    }

//    //判断敌人是否击中了我方坦克
//    public void hitHero(){
//            for(EnemyTank enemytank:enemyTanks){
//                for(Shot shot:enemytank.shots){
//                    if(hero.isLive && shot.isLive)
//                        hitTank(shot,hero);
//                }
//            }
//    }

    //当多颗子弹时，有可能子弹击中会有遗漏，需要遍历所有子弹
    public void hitEnemyTank(){
        for(int j=0;j< hero.shots.size();j++){
            Shot shot = hero.shots.get(j);
            if(hero.shot != null && hero.shot.isLive){
                for(int i = 0; i < enemyTanks.size();i++){
                    EnemyTank enemytank = enemyTanks.get(i);
                    hitTank(shot,enemytank);
                }
            }
        }

    }
//此方法是通用，无论是我方还是敌方，只不过enemyTank变量名是敌方坦克，为
    public void hitTank(Shot s,Tank enemyTank){
        switch (enemyTank.getDirect()){
            case 0: //坦克向上
            case 2: //坦克向下
                if(s.x > enemyTank.getX() && s.x < enemyTank.getX() + 40 && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 60 ){
                    s.isLive = false;
                    enemyTank.isLive = false;
                    System.out.println("tank isLive status:" + enemyTank.isLive);
                    System.out.println(enemyTanks.size());
                    if(enemyTank instanceof  EnemyTank){
                        enemyTanks.remove(enemyTank);
                        Recorder.addALLEnemyTankNum();
                    }

                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);

                }
                break;
            case 1:

                break;
            case 3:
                if(s.x > enemyTank.getX() && s.x < enemyTank.getX() + 60 && s.y > enemyTank.getY() && s.y < enemyTank.getY() + 40 ){
                    s.isLive = false;
                    enemyTank.isLive = false;
                    System.out.println("tank isLive status:" + enemyTank.isLive);
                    if(enemyTank instanceof  EnemyTank){
                        enemyTanks.remove(enemyTank);
                        Recorder.addALLEnemyTankNum();
                    }
                    Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
                    bombs.add(bomb);
                }
                break;
        }
    }
    public void drawTank(int x,int y,Graphics g,int direct,int type){
        switch (type){
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }

        switch (direct){
            case 0://方向向上
                g.fill3DRect(x,y,10,60,false);//画出坦克左边的轮子
                g.fill3DRect(x+30,y,10,60,false);//画出坦克右边的轮子
                g.fill3DRect(x+10,y+10,20,40,false);//画出坦克中间正方形的
                g.fillOval(x+10,y+20,20,20);//坦克中间圆形
                g.drawLine(x+20,y+30,x+20,y);//画出炮筒
                break;
            case 1://方向向右
                g.fill3DRect(x,y,60,10,false);//画出坦克左边的轮子
                g.fill3DRect(x,y+30,60,10,false);//画出坦克右边的轮子
                g.fill3DRect(x+10,y+10,40,20,false);//画出坦克中间正方形的
                g.fillOval(x+20,y+10,20,20);//坦克中间圆形
                g.drawLine(x+30,y+20,x+60,y+20);//画出炮筒
                break;
            case 2://方向向下
                g.fill3DRect(x,y,10,60,false);//画出坦克左边的轮子
                g.fill3DRect(x+30,y,10,60,false);//画出坦克右边的轮子
                g.fill3DRect(x+10,y+10,20,40,false);//画出坦克中间正方形的
                g.fillOval(x+10,y+20,20,20);//坦克中间圆形
                g.drawLine(x+20,y+30,x+20,y+60);//画出炮筒
                break;
            case 3://方向向左边
                g.fill3DRect(x,y,60,10,false);//画出坦克左边的轮子
                g.fill3DRect(x,y+30,60,10,false);//画出坦克右边的轮子
                g.fill3DRect(x+10,y+10,40,20,false);//画出坦克中间正方形的
                g.fillOval(x+20,y+10,20,20);//坦克中间圆形
                g.drawLine(x+30,y+20,x,y+20);//画出炮筒
                break;
            default:
                System.out.println("不处理");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println((char)e.getKeyCode());
        if(e.getKeyCode()==KeyEvent.VK_W){
            hero.setDirect(0);
            if(hero.getY()>0){
                hero.moveUp();
            }

        }else if(e.getKeyCode()==KeyEvent.VK_D){
            hero.setDirect(1);
            if(hero.getX() + 60 < 1000){
                System.out.println(hero.getX()+60);
                hero.moveRight();
            }

        }else if(e.getKeyCode()==KeyEvent.VK_S){
            hero.setDirect(2);
            if(hero.getY()+60<750){
                hero.moveDown();
            }

        }else if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setDirect(3);
            if(hero.getX()>0){
                hero.moveLeft();
            }

        }

        if(e.getKeyCode()==KeyEvent.VK_J){
            //单颗子弹控制方法，当多颗子弹，用vector容器来控制和存放子弹
//            if(hero.shot == null || !hero.shot.isLive){
//                hero.shotEnemyTank();
//            }
              hero.shotEnemyTank();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run(){ //每隔100毫秒，重绘区域，刷新绘图区域，子弹会移动
        while(true){
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            //判断我们是否击中了敌人坦克
            hitEnemyTank();
            hitHero();


            this.repaint();
        }
    }
}
